<?php 
class SurveyQuestion
{
	public $Id;
	public $SurveyId;
	public $SectionId;
	public $Type;
	public $Order;
	public $Active;
	public $Question;
	public $QuestionLong;
	public $Guide;
	public $PossibleAnswers;
	public $MinValue;
	public $MaxValue;
	public $DataChanged;
	
	public function SurveyQuestion() 
	{ self::setDataChanged(false); }
	
	public function getId() { return $this->Id; }
	public function getSurveyId() { return $this->SurveyId; }
	public function getSectionId() { return $this->SectionId; }
	public function getType() { return $this->Type; }
	public function getOrder() { return $this->Order; }
	public function getActive() { return $this->Active; }
	public function getQuestion() { return $this->Question; }
	public function getQuestionLong() { return $this->QuestionLong; }
	public function getGuide() { return $this->Guide; }
	public function getPossibleAnswers() { return $this->PossibleAnswers; }
	public function getMinValue() { return $this->MinValue; }
	public function getMaxValue() { return $this->MaxValue; }
	public function getDataChanged() { return $this->DataChanged; }
	
	public function setId($val) { $this->Id=$val; }
	public function setSurveyId($val) { $this->SurveyId=$val; }
	public function setSectionId($val) { $this->SectionId=$val; }
	public function setType($val) { $this->Type=$val; }
	public function setOrder($val) { $this->Order=$val; }
	public function setActive($val) { $this->Active=$val; }
	public function setQuestion($val) { $this->Question=$val; }
	public function setQuestionLong($val) { $this->QuestionLong=$val; }
	public function setGuide($val) { $this->Guide=$val; }
	public function setPossibleAnswers($val) { $this->PossibleAnswers=$val; }
	public function setMinValue($val) { $this->MinValue=$val; }
	public function setMaxValue($val) {$this->MaxValue=$val; }
	public function setDataChanged($val) { $this->DataChanged=$val; }
	
	public function update()
	{
		if(($this->Id > 0) && ($this->DataChanged == true))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'survey_question SET ';
			$sql .= '`question`=\''.mysql_real_escape_string($this->Question).'\', ';
			$sql .= '`question_type`='.$this->Type.', ';
			$sql .= '`min_val`='.$this->MinValue.', ';
			$sql .= '`max_val`='.$this->MaxValue.' '; 
			$sql .= 'WHERE `id`='.$this->Id.';';
			$result = mysql_query($sql);
		}
	}
	
	public function isValidAnswer($answer)
	{
		if((($answer >= $this->MinValue) && ($answer <= $this->MaxValue)) || ($answer == 0))
		{ return true; }
		else
		{ return false; }
	}
	
	public function getAnsweredValueCount($value, $field='value', $demogs='')
	{
		if($value > 0)
		{
			$sqlDemog = '';
			if(is_array($demogs))
			{
				foreach(array_keys($demogs) as $key)
				{ 
					if($demogs[$key] > 0)
					{ $sqlDemog .=' AND savs.response_id IN (SELECT srdv.response_id FROM '.DB_PERFIX.'survey_response_demog_vals AS srdv WHERE srdv.value='.$demogs[$key].') '; }
				}
			}
			$count = 0;
			$sql  = 'SELECT COUNT(savs.id) AS values_count FROM '.DB_PERFIX.'survey_answer_values_scale AS savs ';
			$sql .= 'WHERE savs.question_id='.$this->Id.' AND savs.'.$field.'='.$value.' '.$sqlDemog.';';

			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ $count = $row['values_count']; }
			}
			return $count;
		}
	}
}
?>